
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>模型 Meta 选项 &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="模型实例参考" href="instances.html" />
    <link rel="prev" title="模型类参考" href="class.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="class.html" title="模型类参考">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="instances.html" title="模型实例参考">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-models-options">
            
  <div class="section" id="s-model-meta-options">
<span id="model-meta-options"></span><h1>模型 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 选项<a class="headerlink" href="#model-meta-options" title="永久链接至标题">¶</a></h1>
<p>本文档解释了所有可能的 <a class="reference internal" href="../../topics/db/models.html#meta-options"><span class="std std-ref">元数据选项</span></a>，你可以在模型的内部 <code class="docutils literal notranslate"><span class="pre">class</span> <span class="pre">Meta</span></code> 中为模型提供这些选项。</p>
<div class="section" id="s-available-meta-options">
<span id="available-meta-options"></span><h2>可用的 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 选项<a class="headerlink" href="#available-meta-options" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-abstract">
<span id="abstract"></span><h3><code class="docutils literal notranslate"><span class="pre">abstract</span></code><a class="headerlink" href="#abstract" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.abstract">
<code class="descclassname">Options.</code><code class="descname">abstract</code><a class="headerlink" href="#django.db.models.Options.abstract" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <code class="docutils literal notranslate"><span class="pre">abstract</span> <span class="pre">=</span> <span class="pre">True</span></code>，这个模型将是一个 <a class="reference internal" href="../../topics/db/models.html#abstract-base-classes"><span class="std std-ref">抽象基类</span></a>。</p>
</dd></dl>

</div>
<div class="section" id="s-app-label">
<span id="app-label"></span><h3><code class="docutils literal notranslate"><span class="pre">app_label</span></code><a class="headerlink" href="#app-label" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.app_label">
<code class="descclassname">Options.</code><code class="descname">app_label</code><a class="headerlink" href="#django.db.models.Options.app_label" title="永久链接至目标">¶</a></dt>
<dd><p>如果在 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 中定义了一个应用程序之外的模型，它必须声明它属于哪个应用程序：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">app_label</span> <span class="o">=</span> <span class="s1">&#39;myapp&#39;</span>
</pre></div>
</div>
<p>如果你想用 <code class="docutils literal notranslate"><span class="pre">app_label.object_name</span></code> 或 <code class="docutils literal notranslate"><span class="pre">app_label.model_name</span></code> 来表示一个模型，你可以分别使用 <code class="docutils literal notranslate"><span class="pre">model._meta.label</span></code> 或 <code class="docutils literal notranslate"><span class="pre">model._meta.label_lower</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-base-manager-name">
<span id="base-manager-name"></span><h3><code class="docutils literal notranslate"><span class="pre">base_manager_name</span></code><a class="headerlink" href="#base-manager-name" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.base_manager_name">
<code class="descclassname">Options.</code><code class="descname">base_manager_name</code><a class="headerlink" href="#django.db.models.Options.base_manager_name" title="永久链接至目标">¶</a></dt>
<dd><p>管理器的属性名，例如，<code class="docutils literal notranslate"><span class="pre">'objects'</span></code>，用于模型的 <a class="reference internal" href="../../topics/db/managers.html#django.db.models.Model._base_manager" title="django.db.models.Model._base_manager"><code class="xref py py-attr docutils literal notranslate"><span class="pre">_base_manager</span></code></a>。</p>
</dd></dl>

</div>
<div class="section" id="s-db-table">
<span id="db-table"></span><h3><code class="docutils literal notranslate"><span class="pre">db_table</span></code><a class="headerlink" href="#db-table" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.db_table">
<code class="descclassname">Options.</code><code class="descname">db_table</code><a class="headerlink" href="#django.db.models.Options.db_table" title="永久链接至目标">¶</a></dt>
<dd><p>用于模型的数据库表的名称：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">db_table</span> <span class="o">=</span> <span class="s1">&#39;music_album&#39;</span>
</pre></div>
</div>
</dd></dl>

<div class="section" id="s-table-names">
<span id="s-id1"></span><span id="table-names"></span><span id="id1"></span><h4>表名称<a class="headerlink" href="#table-names" title="永久链接至标题">¶</a></h4>
<p>为了节省你的时间，Django 会自动从你的模型类和包含它的应用程序的名称中导出数据库表的名称。一个模型的数据库表名是通过将模型的“app label”——你在 <a class="reference internal" href="../django-admin.html#django-admin-startapp"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">manage.py</span> <span class="pre">startapp</span></code></a> 中使用的名称——与模型的类名连接起来，并在两者之间加上下划线。</p>
<p>例如，如果你有一个应用程序 <code class="docutils literal notranslate"><span class="pre">bookstore</span></code> （由 <code class="docutils literal notranslate"><span class="pre">manage.py</span> <span class="pre">startapp</span> <span class="pre">bookstore</span></code> 创建），一个定义为 <code class="docutils literal notranslate"><span class="pre">class</span> <span class="pre">Book</span></code> 的模型将有一个名为 <code class="docutils literal notranslate"><span class="pre">bookstore_book</span></code> 的数据库表。</p>
<p>要覆盖数据库表名，使用 <code class="docutils literal notranslate"><span class="pre">class</span> <span class="pre">Meta</span></code> 中的 <code class="docutils literal notranslate"><span class="pre">db_table</span></code> 参数。</p>
<p>如果你的数据库表名是 SQL 的保留字，或者包含 Python 变量名中不允许的字符——特别是连字符——那也没关系。Django 会在幕后引用列名和表名。</p>
<div class="admonition-use-lowercase-table-names-for-mariadb-and-mysql admonition">
<p class="first admonition-title">在 MariaDB 和 MySQL 中使用小写的表名</p>
<p class="last">当你通过 <code class="docutils literal notranslate"><span class="pre">db_table</span></code> 覆盖表名时，强烈建议你使用小写的表名，特别是当你使用 MySQL 后端时。更多细节请参见 <a class="reference internal" href="../databases.html#mysql-notes"><span class="std std-ref">MySQL 注解</span></a>。</p>
</div>
<div class="admonition-table-name-quoting-for-oracle admonition">
<p class="first admonition-title">Oracle 的表名引用</p>
<p>为了满足 Oracle 对表名的 30 个字符的限制，并符合 Oracle 数据库的惯例，Django 可能会缩短表名，并将其全部变成大写。为了防止这样的转变，使用带引号的名称作为 <code class="docutils literal notranslate"><span class="pre">db_table</span></code> 的值：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">db_table</span> <span class="o">=</span> <span class="s1">&#39;&quot;name_left_in_lowercase&quot;&#39;</span>
</pre></div>
</div>
<p class="last">这样的引号也可以用在 Django 的其他支持的数据库后端，但是除了 Oracle，引号没有任何作用。更多细节请参见 <a class="reference internal" href="../databases.html#oracle-notes"><span class="std std-ref">Oracle 注解</span></a>。</p>
</div>
</div>
</div>
<div class="section" id="s-db-tablespace">
<span id="db-tablespace"></span><h3><code class="docutils literal notranslate"><span class="pre">db_tablespace</span></code><a class="headerlink" href="#db-tablespace" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.db_tablespace">
<code class="descclassname">Options.</code><code class="descname">db_tablespace</code><a class="headerlink" href="#django.db.models.Options.db_tablespace" title="永久链接至目标">¶</a></dt>
<dd><p>此模型要使用的 <a class="reference internal" href="../../topics/db/tablespaces.html"><span class="doc">数据库表空间</span></a> 名称。如果有设置的话，默认是项目的 <a class="reference internal" href="../settings.html#std:setting-DEFAULT_TABLESPACE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEFAULT_TABLESPACE</span></code></a> 配置。如果后端不支持表空间，则忽略此选项。</p>
</dd></dl>

</div>
<div class="section" id="s-default-manager-name">
<span id="default-manager-name"></span><h3><code class="docutils literal notranslate"><span class="pre">default_manager_name</span></code><a class="headerlink" href="#default-manager-name" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.default_manager_name">
<code class="descclassname">Options.</code><code class="descname">default_manager_name</code><a class="headerlink" href="#django.db.models.Options.default_manager_name" title="永久链接至目标">¶</a></dt>
<dd><p>模型的 <a class="reference internal" href="../../topics/db/managers.html#django.db.models.Model._default_manager" title="django.db.models.Model._default_manager"><code class="xref py py-attr docutils literal notranslate"><span class="pre">_default_manager</span></code></a> 管理器名称。</p>
</dd></dl>

</div>
<div class="section" id="s-default-related-name">
<span id="default-related-name"></span><h3><code class="docutils literal notranslate"><span class="pre">default_related_name</span></code><a class="headerlink" href="#default-related-name" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.default_related_name">
<code class="descclassname">Options.</code><code class="descname">default_related_name</code><a class="headerlink" href="#django.db.models.Options.default_related_name" title="永久链接至目标">¶</a></dt>
<dd><p>从相关对象到这个对象的关系默认使用的名称。默认为 <code class="docutils literal notranslate"><span class="pre">_set</span></code>。</p>
<p>这个选项还可以设置 <a class="reference internal" href="fields.html#django.db.models.ForeignKey.related_query_name" title="django.db.models.ForeignKey.related_query_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">related_query_name</span></code></a>。</p>
<p>由于字段的反向名称应该是唯一的，所以如果你打算对你的模型进行子类化，就要小心了。为了避免名称冲突，名称的一部分应该包含 <code class="docutils literal notranslate"><span class="pre">'%(app_label)s'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'%(model_name)s'</span></code>，它们分别被模型所在的应用程序的名称和模型的名称所取代，都是小写的。见 <a class="reference internal" href="../../topics/db/models.html#abstract-related-name"><span class="std std-ref">抽象模型的相关名称</span></a> 段落。</p>
</dd></dl>

</div>
<div class="section" id="s-get-latest-by">
<span id="get-latest-by"></span><h3><code class="docutils literal notranslate"><span class="pre">get_latest_by</span></code><a class="headerlink" href="#get-latest-by" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.get_latest_by">
<code class="descclassname">Options.</code><code class="descname">get_latest_by</code><a class="headerlink" href="#django.db.models.Options.get_latest_by" title="永久链接至目标">¶</a></dt>
<dd><p>模型中的字段名或字段名列表，通常是 <a class="reference internal" href="fields.html#django.db.models.DateField" title="django.db.models.DateField"><code class="xref py py-class docutils literal notranslate"><span class="pre">DateField</span></code></a>，<a class="reference internal" href="fields.html#django.db.models.DateTimeField" title="django.db.models.DateTimeField"><code class="xref py py-class docutils literal notranslate"><span class="pre">DateTimeField</span></code></a> 或 <a class="reference internal" href="fields.html#django.db.models.IntegerField" title="django.db.models.IntegerField"><code class="xref py py-class docutils literal notranslate"><span class="pre">IntegerField</span></code></a>。这指定了在你的模型中使用的默认字段 <a class="reference internal" href="../../topics/db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><code class="xref py py-class docutils literal notranslate"><span class="pre">Manager</span></code></a> 的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">last()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">earliest()</span></code> 方法。</p>
<p>举例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Latest by ascending order_date.</span>
<span class="n">get_latest_by</span> <span class="o">=</span> <span class="s2">&quot;order_date&quot;</span>

<span class="c1"># Latest by priority descending, order_date ascending.</span>
<span class="n">get_latest_by</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;-priority&#39;</span><span class="p">,</span> <span class="s1">&#39;order_date&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>更多内容请参见 <a class="reference internal" href="querysets.html#django.db.models.query.QuerySet.last" title="django.db.models.query.QuerySet.last"><code class="xref py py-meth docutils literal notranslate"><span class="pre">last()</span></code></a> 文档。</p>
</dd></dl>

</div>
<div class="section" id="s-managed">
<span id="managed"></span><h3><code class="docutils literal notranslate"><span class="pre">managed</span></code><a class="headerlink" href="#managed" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.managed">
<code class="descclassname">Options.</code><code class="descname">managed</code><a class="headerlink" href="#django.db.models.Options.managed" title="永久链接至目标">¶</a></dt>
<dd><p>默认为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，意味着 Django 会在 <a class="reference internal" href="../django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">migrate</span></code></a> 中创建相应的数据库表，或者作为迁移的一部分，并作为 <a class="reference internal" href="../django-admin.html#django-admin-flush"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">flush</span></code></a> 管理命令的一部分删除它们。也就是说，Django <em>管理</em> 数据库表的生命周期。</p>
<p>如果 <code class="docutils literal notranslate"><span class="pre">False</span></code>，将不对该模型进行数据库表的创建、修改或删除操作。如果该模型代表一个现有的表或一个通过其他方式创建的数据库视图，这一点很有用。这是在 <code class="docutils literal notranslate"><span class="pre">managed=False</span></code> 时 <em>唯一</em> 的区别。模型处理的所有其他方面都与正常情况完全相同。这包括</p>
<ol class="arabic">
<li><p class="first">如果不声明的话，在模型中增加一个自动主键字段。 为了避免给后面来的代码读者带来困惑，建议在使用非托管模型时，指定你所建模的数据库表的所有列。</p>
</li>
<li><p class="first">如果一个带有 <code class="docutils literal notranslate"><span class="pre">managed=False</span></code> 的模型包含一个 <a class="reference internal" href="fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a> 指向另一个非托管模型，那么多对多连接的中间表也不会被创建。但是，一个托管模型和一个非托管模型之间的中间表会被创建。</p>
<p>如果你需要改变这种默认行为，请将中间表创建为显式模型（根据需要设置 <code class="docutils literal notranslate"><span class="pre">managed</span></code> ），并使用 <a class="reference internal" href="fields.html#django.db.models.ManyToManyField.through" title="django.db.models.ManyToManyField.through"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ManyToManyField.through</span></code></a> 属性让关系使用你的自定义模型。</p>
</li>
</ol>
<p>对于涉及 <code class="docutils literal notranslate"><span class="pre">managed=False</span></code> 模型的测试，你要确保创建正确的表作为测试设置的一部分。</p>
<p>如果你对改变模型类的 Python 级行为感兴趣，你可以使用 <code class="docutils literal notranslate"><span class="pre">managed=False</span></code> 并创建一个现有模型的副本。然而，对于这种情况，有一个更好的方法： <a class="reference internal" href="../../topics/db/models.html#proxy-models"><span class="std std-ref">Proxy models</span></a>。</p>
</dd></dl>

</div>
<div class="section" id="s-order-with-respect-to">
<span id="order-with-respect-to"></span><h3><code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code><a class="headerlink" href="#order-with-respect-to" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.order_with_respect_to">
<code class="descclassname">Options.</code><code class="descname">order_with_respect_to</code><a class="headerlink" href="#django.db.models.Options.order_with_respect_to" title="永久链接至目标">¶</a></dt>
<dd><p>使该对象可以根据给定字段（通常是 <code class="docutils literal notranslate"><span class="pre">ForeignKey</span></code> ）进行排序。这可以用来使相关对象相对于父对象可排序。例如，如果一个 <code class="docutils literal notranslate"><span class="pre">Answer</span></code> 与一个 <code class="docutils literal notranslate"><span class="pre">Question</span></code> 对象相关，而一个问题有多个答案，并且答案的顺序很重要，你可以这样做：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Question</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
    <span class="c1"># ...</span>

<span class="k">class</span> <span class="nc">Answer</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">question</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Question</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
    <span class="c1"># ...</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">order_with_respect_to</span> <span class="o">=</span> <span class="s1">&#39;question&#39;</span>
</pre></div>
</div>
<p>当设置了 <code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code> 时，还提供了两个额外的方法来检索和设置相关对象的顺序：<code class="docutils literal notranslate"><span class="pre">get_RELATED_order()</span></code> 和 <code class="docutils literal notranslate"><span class="pre">set_RELATED_order()</span></code>。<code class="docutils literal notranslate"><span class="pre">get_RELATED_order()</span></code> 和 <code class="docutils literal notranslate"><span class="pre">set_RELATED_order()</span></code>，其中 <code class="docutils literal notranslate"><span class="pre">RELATED</span></code> 是小写的模型名称。例如，假设一个 <code class="docutils literal notranslate"><span class="pre">Question</span></code> 对象有多个相关的 <code class="docutils literal notranslate"><span class="pre">Answer</span></code> 对象，返回的列表包含相关 <code class="docutils literal notranslate"><span class="pre">Answer</span></code> 对象的主键：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">question</span> <span class="o">=</span> <span class="n">Question</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">question</span><span class="o">.</span><span class="n">get_answer_order</span><span class="p">()</span>
<span class="go">[1, 2, 3]</span>
</pre></div>
</div>
<p><cite>Question`</cite> 对象的相关 <code class="docutils literal notranslate"><span class="pre">Answer</span></code> 对象的顺序可以通过传递 <code class="docutils literal notranslate"><span class="pre">Answer</span></code> 主键列表来设置：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">question</span><span class="o">.</span><span class="n">set_answer_order</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
</pre></div>
</div>
<p>相关对象还得到两个方法，<code class="docutils literal notranslate"><span class="pre">get_next_in_order()</span></code> 和 <code class="docutils literal notranslate"><span class="pre">get_previous_in_order()</span></code>，这两个方法可以用来按照正确的顺序访问这些对象。假设 <code class="docutils literal notranslate"><span class="pre">Answer</span></code> 对象按 <code class="docutils literal notranslate"><span class="pre">id</span></code> 排序：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">answer</span> <span class="o">=</span> <span class="n">Answer</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">answer</span><span class="o">.</span><span class="n">get_next_in_order</span><span class="p">()</span>
<span class="go">&lt;Answer: 3&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">answer</span><span class="o">.</span><span class="n">get_previous_in_order</span><span class="p">()</span>
<span class="go">&lt;Answer: 1&gt;</span>
</pre></div>
</div>
</dd></dl>

<div class="admonition-order-with-respect-to-implicitly-sets-the-ordering-option admonition">
<p class="first admonition-title"><code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code> 隐式地设置 <code class="docutils literal notranslate"><span class="pre">ordering</span></code> 选项。</p>
<p class="last">在内部，<code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code> 增加了一个名为 <code class="docutils literal notranslate"><span class="pre">_order</span></code> 的额外字段／数据库列，并将模型的 <a class="reference internal" href="#django.db.models.Options.ordering" title="django.db.models.Options.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ordering</span></code></a> 选项设置为这个字段。因此，<code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code> 和 <code class="docutils literal notranslate"><span class="pre">ordering</span></code> 不能同时使用，<code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code> 所添加的排序将在每次获取这个模型的对象列表时应用。</p>
</div>
<div class="admonition-changing-order-with-respect-to admonition">
<p class="first admonition-title">更改 <code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code></p>
<p class="last">因为 <code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code> 增加了一个新的数据库列，所以如果你在初始 <a class="reference internal" href="../django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">migrate</span></code></a> 之后添加或更改 <code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code>，请务必进行适当的迁移。</p>
</div>
</div>
<div class="section" id="s-ordering">
<span id="ordering"></span><h3><code class="docutils literal notranslate"><span class="pre">ordering</span></code><a class="headerlink" href="#ordering" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.ordering">
<code class="descclassname">Options.</code><code class="descname">ordering</code><a class="headerlink" href="#django.db.models.Options.ordering" title="永久链接至目标">¶</a></dt>
<dd><p>对象的默认排序，用于获取对象列表时：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;-order_date&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>这是一个字符串和／或查询表达式的元组或列表。每一个字符串都是一个字段名，前面有一个可选的“-”字头，表示降序。没有前缀“-”的字段将按升序排列。使用字符串“?”来随机排序。</p>
<p>例如，要按 <code class="docutils literal notranslate"><span class="pre">pub_date</span></code> 字段升序排列，使用以下方法：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;pub_date&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>要按 <code class="docutils literal notranslate"><span class="pre">pub_date</span></code> 降序排列，请使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;-pub_date&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>要按 <code class="docutils literal notranslate"><span class="pre">pub_date</span></code> 降序，然后按 <code class="docutils literal notranslate"><span class="pre">author</span></code> 升序，请使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;-pub_date&#39;</span><span class="p">,</span> <span class="s1">&#39;author&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>你也可以使用 <a class="reference internal" href="expressions.html"><span class="doc">查询表达式</span></a>。要按 <code class="docutils literal notranslate"><span class="pre">author</span></code> 升序排列，并使空值最后排序，请使用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">F</span>

<span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="n">F</span><span class="p">(</span><span class="s1">&#39;author&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">asc</span><span class="p">(</span><span class="n">nulls_last</span><span class="o">=</span><span class="kc">True</span><span class="p">)]</span>
</pre></div>
</div>
</dd></dl>

<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p>排序不是一个免费的操作。你添加到排序中的每个字段都会给你的数据库带来成本。你添加的每个外键都会隐式地包含其所有的默认排序。</p>
<p class="last">如果查询没有指定顺序，那么结果将以未指定的顺序从数据库中返回。只有当按一组字段排序时，才能保证特定的排序，这些字段唯一地标识结果中的每个对象。例如，如果 <code class="docutils literal notranslate"><span class="pre">name</span></code> 字段不是唯一的，那么按它排序就不能保证具有相同名称的对象总是以相同的顺序出现。</p>
</div>
</div>
<div class="section" id="s-permissions">
<span id="permissions"></span><h3><code class="docutils literal notranslate"><span class="pre">permissions</span></code><a class="headerlink" href="#permissions" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.permissions">
<code class="descclassname">Options.</code><code class="descname">permissions</code><a class="headerlink" href="#django.db.models.Options.permissions" title="永久链接至目标">¶</a></dt>
<dd><p>创建此对象时要输入权限表的额外权限。为每个模型自动创建添加、更改、删除和查看权限。这个例子指定了一个额外的权限，<code class="docutils literal notranslate"><span class="pre">can_deliver_pizzas</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">permissions</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;can_deliver_pizzas&#39;</span><span class="p">,</span> <span class="s1">&#39;Can deliver pizzas&#39;</span><span class="p">)]</span>
</pre></div>
</div>
<p>这是一个由二元元组组成的列表或元组，格式为 <code class="docutils literal notranslate"><span class="pre">(permission_code,</span> <span class="pre">human_readable_permission_name)</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-default-permissions">
<span id="default-permissions"></span><h3><code class="docutils literal notranslate"><span class="pre">default_permissions</span></code><a class="headerlink" href="#default-permissions" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.default_permissions">
<code class="descclassname">Options.</code><code class="descname">default_permissions</code><a class="headerlink" href="#django.db.models.Options.default_permissions" title="永久链接至目标">¶</a></dt>
<dd><p>默认值为 <code class="docutils literal notranslate"><span class="pre">('add',</span> <span class="pre">'change',</span> <span class="pre">'delete',</span> <span class="pre">'view')</span></code> 。你可以自定义这个列表，例如，如果你的应用不需要任何默认的权限，可以将其设置为空列表。它必须在模型创建之前通过 <a class="reference internal" href="../django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">migrate</span></code></a> 在模型上指定，以防止任何遗漏的权限被创建。</p>
</dd></dl>

</div>
<div class="section" id="s-proxy">
<span id="proxy"></span><h3><code class="docutils literal notranslate"><span class="pre">proxy</span></code><a class="headerlink" href="#proxy" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.proxy">
<code class="descclassname">Options.</code><code class="descname">proxy</code><a class="headerlink" href="#django.db.models.Options.proxy" title="永久链接至目标">¶</a></dt>
<dd><p>如果 <code class="docutils literal notranslate"><span class="pre">proxy</span> <span class="pre">=</span> <span class="pre">True</span></code>，作为另一个模型子类的模型将被视为 <a class="reference internal" href="../../topics/db/models.html#proxy-models"><span class="std std-ref">代理模型</span></a>。</p>
</dd></dl>

</div>
<div class="section" id="s-required-db-features">
<span id="required-db-features"></span><h3><code class="docutils literal notranslate"><span class="pre">required_db_features</span></code><a class="headerlink" href="#required-db-features" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.required_db_features">
<code class="descclassname">Options.</code><code class="descname">required_db_features</code><a class="headerlink" href="#django.db.models.Options.required_db_features" title="永久链接至目标">¶</a></dt>
<dd><p>当前连接应具备的数据库特征列表，以便在迁移阶段考虑模型。例如，如果你将此列表设置为 <code class="docutils literal notranslate"><span class="pre">['gis_enabled']</span></code>，则模型将只在支持 GIS 的数据库上同步。在使用多个数据库后端进行测试时，跳过一些模型也很有用。避免模型之间的关系，这些模型可能会被创建，也可能不会被创建，因为 ORM 不会处理这个问题。</p>
</dd></dl>

</div>
<div class="section" id="s-required-db-vendor">
<span id="required-db-vendor"></span><h3><code class="docutils literal notranslate"><span class="pre">required_db_vendor</span></code><a class="headerlink" href="#required-db-vendor" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.required_db_vendor">
<code class="descclassname">Options.</code><code class="descname">required_db_vendor</code><a class="headerlink" href="#django.db.models.Options.required_db_vendor" title="永久链接至目标">¶</a></dt>
<dd><p>本模型所特有的支持的数据库厂商名称。目前的内置厂商名称是： <cite>sqlite`</cite>，<cite>postgresql`</cite>，<cite>mysql`</cite> 和 <cite>oracle`</cite>。如果该属性不为空，且当前连接厂商与之不匹配，则该模型将不会同步。</p>
</dd></dl>

</div>
<div class="section" id="s-select-on-save">
<span id="select-on-save"></span><h3><code class="docutils literal notranslate"><span class="pre">select_on_save</span></code><a class="headerlink" href="#select-on-save" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.select_on_save">
<code class="descclassname">Options.</code><code class="descname">select_on_save</code><a class="headerlink" href="#django.db.models.Options.select_on_save" title="永久链接至目标">¶</a></dt>
<dd><p>确定 Django 是否会使用 1.6 之前的 <a class="reference internal" href="instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><code class="xref py py-meth docutils literal notranslate"><span class="pre">django.db.models.Model.save()</span></code></a> 算法。旧的算法使用 <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> 来确定是否有一条现有的记录需要更新。新算法直接尝试 <code class="docutils literal notranslate"><span class="pre">UPDATE</span></code>。在一些罕见的情况下，Django 看不到现有行的 <code class="docutils literal notranslate"><span class="pre">UPDATE</span></code>。例如 PostgreSQL 的 <code class="docutils literal notranslate"><span class="pre">ON</span> <span class="pre">UPDATE</span></code> 触发器会返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。在这种情况下，即使数据库中存在一条记录，新算法最终也会进行 <code class="docutils literal notranslate"><span class="pre">INSERT</span></code>。</p>
<p>通常不需要设置这个属性。默认值是 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
<p>关于新旧保存算法，请参见 <a class="reference internal" href="instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><code class="xref py py-meth docutils literal notranslate"><span class="pre">django.db.models.Model.save()</span></code></a>。</p>
</dd></dl>

</div>
<div class="section" id="s-indexes">
<span id="indexes"></span><h3><code class="docutils literal notranslate"><span class="pre">indexes</span></code><a class="headerlink" href="#indexes" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.indexes">
<code class="descclassname">Options.</code><code class="descname">indexes</code><a class="headerlink" href="#django.db.models.Options.indexes" title="永久链接至目标">¶</a></dt>
<dd><p>你想在模型上定义的 <a class="reference internal" href="indexes.html"><span class="doc">indexes</span></a> 的列表：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Customer</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">first_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">last_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">models</span><span class="o">.</span><span class="n">Index</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;last_name&#39;</span><span class="p">,</span> <span class="s1">&#39;first_name&#39;</span><span class="p">]),</span>
            <span class="n">models</span><span class="o">.</span><span class="n">Index</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;first_name&#39;</span><span class="p">],</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;first_name_idx&#39;</span><span class="p">),</span>
        <span class="p">]</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-unique-together">
<span id="unique-together"></span><h3><code class="docutils literal notranslate"><span class="pre">unique_together</span></code><a class="headerlink" href="#unique-together" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.unique_together">
<code class="descclassname">Options.</code><code class="descname">unique_together</code><a class="headerlink" href="#django.db.models.Options.unique_together" title="永久链接至目标">¶</a></dt>
<dd><div class="admonition-use-class-uniqueconstraint-with-the-attr-options-constraints-option-instead admonition">
<p class="first admonition-title">使用 <a class="reference internal" href="constraints.html#django.db.models.UniqueConstraint" title="django.db.models.UniqueConstraint"><code class="xref py py-class docutils literal notranslate"><span class="pre">UniqueConstraint</span></code></a> 与 <code class="xref py py-attr docutils literal notranslate"><span class="pre">constraint</span></code> 选项代替。</p>
<p class="last"><a class="reference internal" href="constraints.html#django.db.models.UniqueConstraint" title="django.db.models.UniqueConstraint"><code class="xref py py-class docutils literal notranslate"><span class="pre">UniqueConstraint</span></code></a> 比 <code class="docutils literal notranslate"><span class="pre">unique_together</span></code> 提供更多的功能。<code class="docutils literal notranslate"><span class="pre">unique_together</span></code> 今后可能会被废弃。</p>
</div>
<p>一组字段名，合起来必须是唯一的：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">unique_together</span> <span class="o">=</span> <span class="p">[[</span><span class="s1">&#39;driver&#39;</span><span class="p">,</span> <span class="s1">&#39;restaurant&#39;</span><span class="p">]]</span>
</pre></div>
</div>
<p>这是一个列表，这些列表在一起考虑时必须是唯一的。它 在Django 管理中使用，并在数据库级别执行（即在 <code class="docutils literal notranslate"><span class="pre">CREATE</span> <span class="pre">TABLE</span></code> 语句中包含适当的 <code class="docutils literal notranslate"><span class="pre">UNIQUE</span></code> 语句）。</p>
<p>为方便起见，<code class="docutils literal notranslate"><span class="pre">unique_together</span></code> 在处理单组字段时可以是一个单一的列表：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">unique_together</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;driver&#39;</span><span class="p">,</span> <span class="s1">&#39;restaurant&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>一个 <a class="reference internal" href="fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a> 不能被包含在 unique_together 中。（不清楚那意味着什么！）如果你需要验证与 <a class="reference internal" href="fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a> 相关的唯一性，可以尝试使用信号或显式 <a class="reference internal" href="fields.html#django.db.models.ManyToManyField.through" title="django.db.models.ManyToManyField.through"><code class="xref py py-attr docutils literal notranslate"><span class="pre">through</span></code></a> 模型。</p>
<p>在模型验证过程中，当约束条件被违反时引发的 <code class="docutils literal notranslate"><span class="pre">ValidationError</span></code> 具有 <code class="docutils literal notranslate"><span class="pre">unique_together</span></code> 错误代码。</p>
</dd></dl>

</div>
<div class="section" id="s-index-together">
<span id="index-together"></span><h3><code class="docutils literal notranslate"><span class="pre">index_together</span></code><a class="headerlink" href="#index-together" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.index_together">
<code class="descclassname">Options.</code><code class="descname">index_together</code><a class="headerlink" href="#django.db.models.Options.index_together" title="永久链接至目标">¶</a></dt>
<dd><div class="admonition-use-the-attr-options-indexes-option-instead admonition">
<p class="first admonition-title">使用 <a class="reference internal" href="#django.db.models.Options.indexes" title="django.db.models.Options.indexes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">indexes</span></code></a> 选项代替。</p>
<p class="last">新的 <a class="reference internal" href="#django.db.models.Options.indexes" title="django.db.models.Options.indexes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">indexes</span></code></a> 选项比 <code class="docutils literal notranslate"><span class="pre">index_together</span></code> 提供了更多的功能。<code class="docutils literal notranslate"><span class="pre">index_together</span></code> 今后可能会被废弃。</p>
</div>
<p>一组字段名，合在一起，是有索引的：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">index_together</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">[</span><span class="s2">&quot;pub_date&quot;</span><span class="p">,</span> <span class="s2">&quot;deadline&quot;</span><span class="p">],</span>
<span class="p">]</span>
</pre></div>
</div>
<p>该字段清单将被编入索引（即发出适当的 <code class="docutils literal notranslate"><span class="pre">CREATE</span> <span class="pre">INDEX</span></code> 语句）。</p>
<p>为方便起见，<code class="docutils literal notranslate"><span class="pre">index_together</span></code> 在处理一组字段时，可以是一个单一的列表：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">index_together</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;pub_date&quot;</span><span class="p">,</span> <span class="s2">&quot;deadline&quot;</span><span class="p">]</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-constraints">
<span id="constraints"></span><h3><code class="docutils literal notranslate"><span class="pre">constraints</span></code><a class="headerlink" href="#constraints" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.constraints">
<code class="descclassname">Options.</code><code class="descname">constraints</code><a class="headerlink" href="#django.db.models.Options.constraints" title="永久链接至目标">¶</a></dt>
<dd><p>你想在模型上定义的 <a class="reference internal" href="constraints.html"><span class="doc">约束</span></a> 列表：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Customer</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">age</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">constraints</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">models</span><span class="o">.</span><span class="n">CheckConstraint</span><span class="p">(</span><span class="n">check</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">Q</span><span class="p">(</span><span class="n">age__gte</span><span class="o">=</span><span class="mi">18</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;age_gte_18&#39;</span><span class="p">),</span>
        <span class="p">]</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-verbose-name">
<span id="verbose-name"></span><h3><code class="docutils literal notranslate"><span class="pre">verbose_name</span></code><a class="headerlink" href="#verbose-name" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.verbose_name">
<code class="descclassname">Options.</code><code class="descname">verbose_name</code><a class="headerlink" href="#django.db.models.Options.verbose_name" title="永久链接至目标">¶</a></dt>
<dd><p>对象的可读名称，单数：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">verbose_name</span> <span class="o">=</span> <span class="s2">&quot;pizza&quot;</span>
</pre></div>
</div>
<p>如果没有给定，Django 将使用一个 munged 版本的类名：<code class="docutils literal notranslate"><span class="pre">CamelCase</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">camel</span> <span class="pre">case</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-verbose-name-plural">
<span id="verbose-name-plural"></span><h3><code class="docutils literal notranslate"><span class="pre">verbose_name_plural</span></code><a class="headerlink" href="#verbose-name-plural" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.verbose_name_plural">
<code class="descclassname">Options.</code><code class="descname">verbose_name_plural</code><a class="headerlink" href="#django.db.models.Options.verbose_name_plural" title="永久链接至目标">¶</a></dt>
<dd><p>对象的复数名称：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="s2">&quot;stories&quot;</span>
</pre></div>
</div>
<p>如果没有给定，Django 将使用 <a class="reference internal" href="#django.db.models.Options.verbose_name" title="django.db.models.Options.verbose_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">verbose_name</span></code></a> + <code class="docutils literal notranslate"><span class="pre">&quot;s&quot;</span></code>。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-read-only-meta-attributes">
<span id="read-only-meta-attributes"></span><h2>只读的 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 属性<a class="headerlink" href="#read-only-meta-attributes" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-label">
<span id="label"></span><h3><code class="docutils literal notranslate"><span class="pre">label</span></code><a class="headerlink" href="#label" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.label">
<code class="descclassname">Options.</code><code class="descname">label</code><a class="headerlink" href="#django.db.models.Options.label" title="永久链接至目标">¶</a></dt>
<dd><p>对象的表示，返回 <code class="docutils literal notranslate"><span class="pre">app_label.object_name</span></code>，例如 <code class="docutils literal notranslate"><span class="pre">'polls.Question'</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-label-lower">
<span id="label-lower"></span><h3><code class="docutils literal notranslate"><span class="pre">label_lower</span></code><a class="headerlink" href="#label-lower" title="永久链接至标题">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Options.label_lower">
<code class="descclassname">Options.</code><code class="descname">label_lower</code><a class="headerlink" href="#django.db.models.Options.label_lower" title="永久链接至目标">¶</a></dt>
<dd><p>模型的表示，返回 <code class="docutils literal notranslate"><span class="pre">app_label.model_name</span></code>，例如 <code class="docutils literal notranslate"><span class="pre">'polls.question'</span></code>。</p>
</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">模型 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 选项</a><ul>
<li><a class="reference internal" href="#available-meta-options">可用的 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 选项</a><ul>
<li><a class="reference internal" href="#abstract"><code class="docutils literal notranslate"><span class="pre">abstract</span></code></a></li>
<li><a class="reference internal" href="#app-label"><code class="docutils literal notranslate"><span class="pre">app_label</span></code></a></li>
<li><a class="reference internal" href="#base-manager-name"><code class="docutils literal notranslate"><span class="pre">base_manager_name</span></code></a></li>
<li><a class="reference internal" href="#db-table"><code class="docutils literal notranslate"><span class="pre">db_table</span></code></a><ul>
<li><a class="reference internal" href="#table-names">表名称</a></li>
</ul>
</li>
<li><a class="reference internal" href="#db-tablespace"><code class="docutils literal notranslate"><span class="pre">db_tablespace</span></code></a></li>
<li><a class="reference internal" href="#default-manager-name"><code class="docutils literal notranslate"><span class="pre">default_manager_name</span></code></a></li>
<li><a class="reference internal" href="#default-related-name"><code class="docutils literal notranslate"><span class="pre">default_related_name</span></code></a></li>
<li><a class="reference internal" href="#get-latest-by"><code class="docutils literal notranslate"><span class="pre">get_latest_by</span></code></a></li>
<li><a class="reference internal" href="#managed"><code class="docutils literal notranslate"><span class="pre">managed</span></code></a></li>
<li><a class="reference internal" href="#order-with-respect-to"><code class="docutils literal notranslate"><span class="pre">order_with_respect_to</span></code></a></li>
<li><a class="reference internal" href="#ordering"><code class="docutils literal notranslate"><span class="pre">ordering</span></code></a></li>
<li><a class="reference internal" href="#permissions"><code class="docutils literal notranslate"><span class="pre">permissions</span></code></a></li>
<li><a class="reference internal" href="#default-permissions"><code class="docutils literal notranslate"><span class="pre">default_permissions</span></code></a></li>
<li><a class="reference internal" href="#proxy"><code class="docutils literal notranslate"><span class="pre">proxy</span></code></a></li>
<li><a class="reference internal" href="#required-db-features"><code class="docutils literal notranslate"><span class="pre">required_db_features</span></code></a></li>
<li><a class="reference internal" href="#required-db-vendor"><code class="docutils literal notranslate"><span class="pre">required_db_vendor</span></code></a></li>
<li><a class="reference internal" href="#select-on-save"><code class="docutils literal notranslate"><span class="pre">select_on_save</span></code></a></li>
<li><a class="reference internal" href="#indexes"><code class="docutils literal notranslate"><span class="pre">indexes</span></code></a></li>
<li><a class="reference internal" href="#unique-together"><code class="docutils literal notranslate"><span class="pre">unique_together</span></code></a></li>
<li><a class="reference internal" href="#index-together"><code class="docutils literal notranslate"><span class="pre">index_together</span></code></a></li>
<li><a class="reference internal" href="#constraints"><code class="docutils literal notranslate"><span class="pre">constraints</span></code></a></li>
<li><a class="reference internal" href="#verbose-name"><code class="docutils literal notranslate"><span class="pre">verbose_name</span></code></a></li>
<li><a class="reference internal" href="#verbose-name-plural"><code class="docutils literal notranslate"><span class="pre">verbose_name_plural</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#read-only-meta-attributes">只读的 <code class="docutils literal notranslate"><span class="pre">Meta</span></code> 属性</a><ul>
<li><a class="reference internal" href="#label"><code class="docutils literal notranslate"><span class="pre">label</span></code></a></li>
<li><a class="reference internal" href="#label-lower"><code class="docutils literal notranslate"><span class="pre">label_lower</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="class.html"
                        title="上一章">模型类参考</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="instances.html"
                        title="下一章">模型实例参考</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/models/options.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="class.html" title="模型类参考">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="instances.html" title="模型实例参考">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>